package testExport;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import test.ExportExcel;
import testEntity.FreeCarExcle;

import com.cz073586.tools.util.Tools;
import com.local.model.FreeCar;
import com.local.page.FreeCarPage;
import com.local.service.face.FreeCarService;
import com.qianhai.common.spring.utils.SpringContextUtil;
import com.qianhai.common.utils.BeanUtil;

public class ExcelFreeCarServlet extends HttpServlet
{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		doPost(req, resp);
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		// 取得文件名。
		String filename = "免费卡信息表";
		// 清空response
		resp.reset();
		// 设置response的Header
		resp.addHeader("Content-Disposition", "attachment;filename="
				+ new String(filename.getBytes("gb2312"), "ISO8859-1" )+".xls");
		resp.setContentType("application/vnd.ms-excel;charset=gb2312");
		
		// 初始化数据
		FreeCarService freeCarService = SpringContextUtil.getApplicationContext().getBean(FreeCarService.class);
		
		FreeCarPage<FreeCar> pageInfo=new FreeCarPage<FreeCar>();
		pageInfo.setPageSize(99999);
		pageInfo.setName(!Tools.isEmpty(req.getParameter("name"))?req.getParameter("name"):null);
		pageInfo.setCarNo(!Tools.isEmpty(req.getParameter("carNo"))?req.getParameter("carNo"):null);
		pageInfo.setStatus(!Tools.isEmpty(req.getParameter("status"))?Integer.parseInt(req.getParameter("status")):0);
		List<FreeCar> list = freeCarService.queryFreeCarQueryForm(pageInfo);
		
		ExportExcel<FreeCarExcle> ex = new ExportExcel<FreeCarExcle>();
		String[] headers = { "车主", "车牌号", "会员类型", "联系方式", "地址", "失效时间"};
		
		List<FreeCarExcle> dataset = new ArrayList<FreeCarExcle>();
		for (FreeCar fc : list)
		{
			FreeCarExcle fe=new FreeCarExcle();
			BeanUtil.copyProperties(fe, fc);
			fe.setType("免费卡");
			dataset.add(fe);
		}
		OutputStream out=resp.getOutputStream();
		ex.exportExcel(filename,headers, dataset, out);
		out.close();  
	}
}